/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai.topic;

import com.jfinal.plugin.activerecord.Page;
import top.hiai.common.account.UserService;
import top.hiai.common.model.Comments;

import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class TopIcService {
	
	private Comments commentsDao = new Comments().dao();
	
	/**
	 * 获取所有的文章分类话题，分页显示，并统计每个分类下面有多少文章
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Comments> commentsPage(int pageNumber, int pageSize){
		//select c.name, count(a.aid) from bbs_comments as c left join bbs_article as a on c.tid=a.type_id group by c.tid;
		String select = " select c.tid,c.name,c.images,count(a.aid) as msgCount  ";
		String from = " from bbs_comments as c left join bbs_article as a on c.tid=a.type_id and a.status=1 and a.is_del=0 group by c.tid order by c.sort desc,c.favoriteCount desc ";
		Page<Comments> commentsList = commentsDao.paginate(pageNumber, pageSize, select,from);
		return commentsList;
	}
	
    /**
     * 更过用户ID获取该分类的信息
     * @param uid
     * @return
     */
	public Comments findById(int cid) {
		return commentsDao.findById(cid);
	}
	/**
	 * 根绝前台传过来的ID获取话题的分类,然后显示该分类下面的所有文章
	 * @return
	 */
	public Page<Comments> getDetailCommentsArticle(int pageNumber, int pageSize,int tid){
		String select = "select a.aid,a.title,a.addtime,a.views,a.uid,a.favoriteCount,a.replyCount,c.name,c.tid ";
		String from = " from bbs_article as a , bbs_comments as c where c.tid=? and a.type_id=c.tid and a.status=1 and a.is_del=0 order by a.addtime desc ";
		Page<Comments> commentsList = commentsDao.paginate(pageNumber, pageSize, select,from ,tid);
        UserService.me.join("uid", commentsList.getList(), "nickName", "avatar","is_auth");
		return commentsList;
	}
	
	public List<Comments> getByConnentsFavoriteUser(int tid){
		//	select c.tid,c.name from bbs_comments as c , bbs_favorite as f where f.refType=2 and f.refId=c.tid and c.tid=1;
		List<Comments> commentsList = commentsDao.find("select c.tid,c.name,f.accountId "
				+ " from bbs_comments as c , bbs_favorite as f "
				+ " where f.refType=2 and f.refId=c.tid and c.tid=?",tid);
		UserService.me.join("accountId", commentsList, "nickName", "avatar","is_auth");
		return commentsList;
	}

}